package org.yinxianren.www.study_02.study.arithmetic;

import org.junit.Test;

import java.util.Arrays;
import java.util.Random;

public class dichotomy  implements IPrint{

    private static int rank(int key,int[] array){
        int lo=0;
        int hi=array.length-1;
        while(lo <= hi){
            int mid = lo + (hi-lo)/2;
            if (key < array[mid]) hi = mid - 1;
            else if(key > array[mid]) lo = mid + 1;
            else return mid;
        }
        return -1;
    }

    @Test
    public void test_01(){
        int []  test={1,52,411,589,574,598,601,603,608,698,890,891,892,893,894,900,901,910};
        int result= rank(698,test);
        System.out.println(result);
    }

    @Test
    public void test_02(){
        int f=0;
        int g=1;
        for(int i=0;i<=15;i++){
            System.out.println(f);
            f=f+g;
            g=f-g;
        }
    }

    @Test
    public void test_03(){
        Arrays.stream( progressiveIncrease(18) )
                .forEach(System.out::println);
    }

    /**
     * 1
     * 1
     * 2
     * 3
     * 5
     * 8
     * 13
     * 21
     * 34
     * 55
     * 89
     * 144
     * 233
     * 377
     * 610
     * 987
     * 1597
     * 2584
     */
    public int[] progressiveIncrease(int n){
        if(n <= 2)  return new int[]{1,1};
        else {
            int i=0;
            int[] sulet= new int[n];
            while(i < n){
                if(i <2) sulet[i] = 1;
                else sulet[i] = sulet[i-2]+sulet[i-1];
                i++;
            }
            return sulet;
        }
    }

    @Test
    public void test_04(){
        double t=9.0;
        while(Math.abs(t-9.0/t) > .001)
            t=(9.0/t + t) / 0.2;
        println(t);//结果：无限死循环
    }

    @Test
    public void test_05(){
        int sum = 0;
        for ( int i = 1 ; i < 1000 ; i++ )
            for(int  j = 0 ; j < i ; j++)
                sum++;

        println(sum);//499500
    }

    @Test
    public void test_06(){
        int sum = 0;
        for ( int i = 1 ; i < 1000 ; i *=2 )
            for(int  j = 0 ; j < i ; j++)
                sum++;

        println(sum);//1023  比原来少了488倍
    }

    @Test
    public void test_07(){
        int sum = 0;
        for ( int i = 1 ; i < 1000 ; i *= 3 )
            for(int  j = 0 ; j < i ; j++)
                sum++;

        println(sum);//1093
    }

    @Test
    public void test_08(){
        int sum = 0;
        for ( int i = 1 ; i < 1000 ; i *= 4 )
            for(int  j = 0 ; j < i ; j++)
                sum++;

        println(sum);//341
    }

    @Test
    public void test_09(){
//        println((char) ('a'+4));  //e
        int N=50;
        println(Integer.toBinaryString(N));
        String S="";
        for(int i=N; i>0 ; i/=2)
            S=(i%2)+S;
        println(S);
    }
/*
N  0  1  2  3  4  5  6  7  8  9
0              *  *        *  *
1     *     *  *  *  *  *  *
2        *     *  *
3     *     *        *     *  *
4  *  *  *        *        *  *
5     *     *  *  *  *
6     *  *        *           *
7        *  *        *  *     *
8           *     *  *  *  *
9  *  *     *     *     *  *  *

 */
    @Test
    public void test_10(){
        boolean[][] test=new boolean[10][10];
        Random  rd=new Random();
        for(int i=0;i<test.length;i++){
            for(int j=0;j<test[i].length;j++){
                test[i][j]=rd.nextBoolean();
            }
        }
        print(String.format("%-3s","N"));
        for(int i=0;i<test.length; i++){
            print(String.format("%-3s",i));
        }
        println("");
        for(int i=0;i<test.length;i++){
            print(String.format("%-3s",i));
            for(int j=0;j<test[i].length;j++){
               if(test[i][j])  print(String.format("%-3s","*"));
               else print(String.format("%-3s",""));
            }
            println("");
        }

    }

    /**
     * 接受一个整型a[]数组 和一个 整型M为参数，并返回一个大小为M的数组，其中第i个元素的值为整型i参数数组中出现的次数；
     * 如果a[]中的值均为0至M-1之间，放回数组中所有元素之和应该和a.length相等
     */
    @Test
    public void test_11(){
        int n=new Random().nextInt();


    }

    private int[]  histogram(int[] a, int m){
        int[] result= new int[m];

        return result;
    }




}
